<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,IE=9,chrome=1"><meta name="generator" content="MATLAB R2020a"><title>Yalmip 利用 gurobi求解器 （mac） </title><style type="text/css">.rtcContent { padding: 30px; } .S0 { margin: 3px 10px 5px 4px; padding: 0px; line-height: 28.8px; min-height: 0px; white-space: pre-wrap; color: rgb(213, 80, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 24px; font-weight: 400; text-align: center;  }
.S1 { margin: 15px 10px 5px 4px; padding: 0px; line-height: 18px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17.9104px; font-weight: 700; text-align: left;  }
.S2 { margin: 20px 10px 5px 4px; padding: 0px; line-height: 20px; min-height: 0px; white-space: pre-wrap; color: rgb(60, 60, 60); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 20px; font-weight: 700; text-align: left;  }
.S3 { margin: 2px 10px 9px 4px; padding: 0px; line-height: 26.8657px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17.9104px; font-weight: 400; text-align: left;  }
.S4 { margin: 2px 10px 9px 4px; padding: 0px; line-height: 26.8657px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17.9104px; font-weight: 400; text-align: center;  }
.CodeBlock { background-color: #F7F7F7; margin: 10px 0 10px 0;}
.S5 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0.979478px solid rgb(233, 233, 233); border-bottom: 0px none rgb(0, 0, 0); border-radius: 4px 4px 0px 0px; padding: 6px 45px 0px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }
.S6 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0px none rgb(0, 0, 0); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 0px 45px 0px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }
.S7 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0px none rgb(0, 0, 0); border-bottom: 0.979478px solid rgb(233, 233, 233); border-radius: 0px; padding: 0px 45px 4px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }
.S8 { color: rgb(64, 64, 64); padding: 10px 0px 6px 17px; background: rgb(255, 255, 255) none repeat scroll 0% 0% / auto padding-box border-box; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; overflow-x: hidden; line-height: 22.0478px;  }
.embeddedOutputsErrorElement {min-height: 18px; max-height: 250px; overflow: auto;}
.embeddedOutputsErrorElement.inlineElement {}
.embeddedOutputsErrorElement.rightPaneElement {}
.embeddedOutputsWarningElement{min-height: 18px; max-height: 250px; overflow: auto;}
.embeddedOutputsWarningElement.inlineElement {}
.embeddedOutputsWarningElement.rightPaneElement {}
.diagnosticMessage-wrapper {font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12px;}
.diagnosticMessage-wrapper.diagnosticMessage-warningType {color: rgb(255,100,0);}
.diagnosticMessage-wrapper.diagnosticMessage-warningType a {color: rgb(255,100,0); text-decoration: underline;}
.diagnosticMessage-wrapper.diagnosticMessage-errorType {color: rgb(230,0,0);}
.diagnosticMessage-wrapper.diagnosticMessage-errorType a {color: rgb(230,0,0); text-decoration: underline;}
.diagnosticMessage-wrapper .diagnosticMessage-messagePart,.diagnosticMessage-wrapper .diagnosticMessage-causePart {white-space: pre-wrap;}
.diagnosticMessage-wrapper .diagnosticMessage-stackPart {white-space: pre;}
.embeddedOutputsTextElement,.embeddedOutputsVariableStringElement {white-space: pre; word-wrap: initial; min-height: 18px; max-height: 250px; overflow: auto;}
.textElement,.rtcDataTipElement .textElement {padding-top: 3px;}
.embeddedOutputsTextElement.inlineElement,.embeddedOutputsVariableStringElement.inlineElement {}
.inlineElement .textElement {}
.embeddedOutputsTextElement.rightPaneElement,.embeddedOutputsVariableStringElement.rightPaneElement {min-height: 16px;}
.rightPaneElement .textElement {padding-top: 2px; padding-left: 9px;}
.S9 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0.979478px solid rgb(233, 233, 233); border-bottom: 0px none rgb(0, 0, 0); border-radius: 0px; padding: 6px 45px 0px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }
.variableValue { width: 100% !important; }
.embeddedOutputsMatrixElement,.eoOutputWrapper .matrixElement {min-height: 18px; box-sizing: border-box;}
.embeddedOutputsMatrixElement .matrixElement,.eoOutputWrapper .matrixElement,.rtcDataTipElement .matrixElement {position: relative;}
.matrixElement .variableValue,.rtcDataTipElement .matrixElement .variableValue {white-space: pre; display: inline-block; vertical-align: top; overflow: hidden;}
.embeddedOutputsMatrixElement.inlineElement {}
.embeddedOutputsMatrixElement.inlineElement .topHeaderWrapper {display: none;}
.embeddedOutputsMatrixElement.inlineElement .veTable .body {padding-top: 0 !important; max-height: 100px;}
.inlineElement .matrixElement {max-height: 300px;}
.embeddedOutputsMatrixElement.rightPaneElement {}
.rightPaneElement .matrixElement,.rtcDataTipElement .matrixElement {overflow: hidden; padding-left: 9px;}
.rightPaneElement .matrixElement {margin-bottom: -1px;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer,.eoOutputWrapper .matrixElement .valueContainer,.rtcDataTipElement .matrixElement .valueContainer {white-space: nowrap; margin-bottom: 3px;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis.hide,.embeddedOutputsMatrixElement .matrixElement .verticalEllipsis.hide,.eoOutputWrapper .matrixElement .valueContainer .horizontalEllipsis.hide,.eoOutputWrapper .matrixElement .verticalEllipsis.hide,.rtcDataTipElement .matrixElement .valueContainer .horizontalEllipsis.hide,.rtcDataTipElement .matrixElement .verticalEllipsis.hide {display: none;}
.embeddedOutputsVariableMatrixElement .matrixElement .valueContainer.hideEllipses .verticalEllipsis, .embeddedOutputsVariableMatrixElement .matrixElement .valueContainer.hideEllipses .horizontalEllipsis {display:none;}
.embeddedOutputsMatrixElement .matrixElement .valueContainer .horizontalEllipsis,.eoOutputWrapper .matrixElement .valueContainer .horizontalEllipsis {margin-bottom: -3px;}
.eoOutputWrapper .embeddedOutputsVariableMatrixElement .matrixElement .valueContainer {cursor: default !important;}
.embeddedOutputsVariableElement {white-space: pre-wrap; word-wrap: break-word; min-height: 18px; max-height: 250px; overflow: auto;}
.variableElement {}
.embeddedOutputsVariableElement.inlineElement {}
.inlineElement .variableElement {}
.embeddedOutputsVariableElement.rightPaneElement {min-height: 16px;}
.rightPaneElement .variableElement {padding-top: 2px; padding-left: 9px;}
.variableNameElement {margin-bottom: 3px; display: inline-block;}
.matrixElement .horizontalEllipsis,.rtcDataTipElement .matrixElement .horizontalEllipsis {display: inline-block; margin-top: 3px; width: 30px; height: 12px; background-repeat: no-repeat; background-image: url("");}
.matrixElement .verticalEllipsis,.textElement .verticalEllipsis,.rtcDataTipElement .matrixElement .verticalEllipsis,.rtcDataTipElement .textElement .verticalEllipsis {margin-left: 35px; width: 12px; height: 30px; background-repeat: no-repeat; background-image: url("");}
.S10 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0.979478px solid rgb(233, 233, 233); border-bottom: 0.979478px solid rgb(233, 233, 233); border-radius: 0px 0px 4px 4px; padding: 6px 45px 4px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }
.S11 { margin: 10px 10px 9px 4px; padding: 0px; line-height: 26.8657px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 17.9104px; font-weight: 400; text-align: left;  }
.S12 { margin: 10px 0px 20px; padding-left: 0px; font-family: Helvetica, Arial, sans-serif; font-size: 17.9104px;  }
.S13 { margin-left: 71.6418px; line-height: 26.8657px; min-height: 0px; text-align: left; white-space: pre-wrap;  }
.S14 { border-left: 0.979478px solid rgb(233, 233, 233); border-right: 0.979478px solid rgb(233, 233, 233); border-top: 0.979478px solid rgb(233, 233, 233); border-bottom: 0.979478px solid rgb(233, 233, 233); border-radius: 0px; padding: 6px 45px 4px 13px; line-height: 22.0478px; min-height: 18px; white-space: nowrap; color: rgb(0, 0, 0); font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px;  }</style></head><body><div class = rtcContent><h1  class = 'S0'><span>Yalmip 利用 gurobi求解器 （mac） </span></h1><h3  class = 'S1'><span></span></h3><h2  class = 'S2'><span>案例1</span></h2><div  class = 'S3'><span></span></div><div  class = 'S4'><span texencoding="max  \quad \frac{ x_{1} + x_{2}}{ (2x_1 +x_2)} \\
s.t.  \begin{cases}
\quad  x_1 + x_2 \geq 2\\
\quad x_2 - x_1 \leq 1\\
 \quad x_1 \leq 1
\end{cases}" style="vertical-align:-56px"><img src="" width="138" height="130.5" /></span></div><div  class = 'S3'><span></span></div><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%% 案例1 </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 清除工作区</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc; clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>;</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 创建决策变量</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>x = sdpvar(1,2);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 添加约束条件</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>C = [</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    x(1) + x(2)  &gt;= 2</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    x(2)-x(1) &lt;=1</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    x(1)&lt;=1</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    ];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 配置</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>ops = sdpsettings(</span><span style="color: rgb(160, 32, 240);">'verbose'</span><span>,1,</span><span style="color: rgb(160, 32, 240);">'solver'</span><span>,</span><span style="color: rgb(160, 32, 240);">'gurobi'</span><span>);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 目标函数</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>z = -(x(1)+2*x(2))/(2*x(1)+x(2)); </span><span style="color: rgb(60, 118, 61);">% 注意这是求解最大值</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 求解</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>reuslt = optimize(C,z);</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="DA69E341" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_0" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="650" data-hashorizontaloverflow="false"><div class="textElement">                                            First-order      Norm of
 Iter F-count            f(x)  Feasibility   optimality         step
    0       1    0.000000e+00    2.000e+00    1.845e+00
    1       2   -1.361410e+00    5.242e-01    7.089e-01    1.602e+00
    2       3   -1.308814e+00    4.441e-16    7.788e-01    8.140e-01
    3       4   -1.299288e+00    0.000e+00    9.851e-02    8.490e-02
    4       5   -1.333584e+00    4.441e-16    1.095e-01    7.327e-02
    5       6   -1.364417e+00    4.441e-16    2.942e-02    7.491e-02
    6       7   -1.360272e+00    0.000e+00    2.021e-02    8.267e-03
    7       8   -1.379966e+00    4.441e-16    2.129e-02    5.706e-02
    8       9   -1.387834e+00    4.441e-16    3.065e-02    5.399e-02
    9      11   -1.391538e+00    0.000e+00    3.738e-02    2.721e-02
   10      12   -1.392332e+00    0.000e+00    4.000e-03    6.714e-03
   11      13   -1.398128e+00    4.441e-16    7.445e-03    2.336e-02
   12      14   -1.398413e+00    0.000e+00    3.518e-03    6.077e-04
   13      15   -1.398400e+00    0.000e+00    8.000e-04    2.893e-05
   14      16   -1.399667e+00    4.441e-16    1.513e-03    5.037e-03
   15      17   -1.399680e+00    0.000e+00    2.062e-04    1.052e-04
   16      18   -1.399680e+00    4.441e-16    1.600e-04    1.719e-06
   17      19   -1.399935e+00    0.000e+00    3.110e-04    1.024e-03
   18      20   -1.399936e+00    4.441e-16    3.200e-05    9.170e-07
   19      21   -1.399999e+00    0.000e+00    7.509e-05    2.535e-04
   20      22   -1.399999e+00    4.441e-16    3.200e-07    3.477e-08

Local minimum found that satisfies the constraints.

Optimization completed because the objective function is non-decreasing in 
feasible directions, to within the value of the optimality tolerance,
and constraints are satisfied to within the value of the constraint tolerance.

&lt;stopping criteria details&gt;</div></div></div></div><div class="inlineWrapper"><div  class = 'S9'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>reuslt.problem == 0 </span><span style="color: rgb(60, 118, 61);">% problem =0 代表求解成功</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    -value(z)   </span><span style="color: rgb(60, 118, 61);">% 反转</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    disp(</span><span style="color: rgb(160, 32, 240);">'求解出错'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="3C54E966" contenteditable="false" data-testid="output_1" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">ans = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">1×2</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:108,&quot;totalColumns&quot;:2,&quot;totalRows&quot;:1,&quot;charsPerColumn&quot;:10}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 218px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">    0.5000    1.5000
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; '>ans = 1.4000</div></div></div><div class="inlineWrapper"><div  class = 'S10'></div></div></div><div  class = 'S11'><span></span></div><h2  class = 'S2'><span>案例2 </span></h2><ul  class = 'S12'><li  class = 'S13'><span> 这是yalmip官网的例子，不在给出显示表达式</span></li></ul><div  class = 'S3'><span></span></div><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%%</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc; clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>; yalmip(</span><span style="color: rgb(160, 32, 240);">'clear'</span><span>);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define variables</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>x = sdpvar(10,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define constraints </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Constraints = [sum(x) &lt;= 10, x(1) == 0, 0.5 &lt;= x(2) &lt;= 1.5];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">for </span><span>i = 1 : 7</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>  </span><span class="warning_squiggle_rte376400421">Constraints</span><span> = [Constraints, x(i) + x(i+1) &lt;= x(i+2) + x(i+3)];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define an objective</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Objective = x'*x+norm(x,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Set some options for YALMIP and solver</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>options = sdpsettings(</span><span style="color: rgb(160, 32, 240);">'verbose'</span><span>,1,</span><span style="color: rgb(160, 32, 240);">'solver'</span><span>,</span><span style="color: rgb(160, 32, 240);">'gurobi'</span><span>);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Solve the problem</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>sol = optimize(Constraints,Objective,options);</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="45B421AE" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_3" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="922" data-hashorizontaloverflow="false"><div class="textElement">Academic license - for non-commercial use only - expires 2021-07-16
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 32 rows, 21 columns and 92 nonzeros
Model fingerprint: 0x92439cb2
Model has 10 quadratic objective terms
Coefficient statistics:
  Matrix range     [1e+00, 1e+00]
  Objective range  [1e+00, 1e+00]
  QObjective range [2e+00, 2e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [5e-01, 1e+01]
Presolve removed 16 rows and 5 columns
Presolve time: 0.06s
Presolved: 16 rows, 16 columns, 49 nonzeros
Presolved model has 8 quadratic objective terms
Ordering time: 0.00s

Barrier statistics:
 Free vars  : 8
 AA' NZ     : 5.500e+01
 Factor NZ  : 1.360e+02
 Factor Ops : 1.496e+03 (less than 1 second per iteration)
 Threads    : 1

                  Objective                Residual
Iter       Primal          Dual         Primal    Dual     Compl     Time
   0   8.00075000e+03  7.49999999e-01  1.00e+00 5.00e-01  1.00e+06     0s
   1   1.45735160e+05 -1.44280002e+05  1.38e+02 9.27e+01  1.54e+05     0s
   2   5.31337943e+04 -5.22500082e+04  3.25e+00 3.32e+00  7.83e+03     0s
   3   1.31069659e+04 -1.03577063e+04  0.00e+00 1.78e-09  9.78e+02     0s
   4   1.21880600e+03 -1.17414049e+03  0.00e+00 5.80e-11  9.97e+01     0s
   5   4.78953496e+02 -3.07689602e+02  0.00e+00 9.77e-15  3.28e+01     0s
   6   3.82139436e+01 -1.89732577e+01  0.00e+00 6.66e-15  2.38e+00     0s
   7   1.67828858e+01 -2.60681220e+00  0.00e+00 5.55e-16  8.08e-01     0s
   8   4.45901567e+00  2.47098963e+00  0.00e+00 5.00e-16  8.28e-02     0s
   9   3.64467687e+00  3.21224569e+00  0.00e+00 5.55e-16  1.80e-02     0s
  10   3.37829219e+00  3.31133007e+00  0.00e+00 4.44e-16  2.79e-03     0s
  11   3.33382936e+00  3.33326160e+00  0.00e+00 5.55e-16  2.37e-05     0s
  12   3.33333383e+00  3.33333326e+00  0.00e+00 7.22e-16  2.37e-08     0s
  13   3.33333333e+00  3.33333333e+00  0.00e+00 7.22e-16  2.37e-11     0s

Barrier solved model in 13 iterations and 0.08 seconds
Optimal objective 3.33333333e+00</div></div></div></div><div class="inlineWrapper"><div  class = 'S9'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Analyze error flags</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>sol.problem == 0</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> </span><span style="color: rgb(60, 118, 61);">% Extract and display value</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> solution = value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> </span><span class="warning_squiggle_rte376400421 warningHighlight376400421">display</span><span>(</span><span style="color: rgb(160, 32, 240);">'Hmm, something went wrong!'</span><span>);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> sol.info</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span> yalmiperror(sol.problem)</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="5AC2748E" contenteditable="false" data-testid="output_4" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">solution = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">10×1</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:108,&quot;totalColumns&quot;:1,&quot;totalRows&quot;:10,&quot;charsPerColumn&quot;:10}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 110px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">         0
    0.5000
    0.0833
    0.4167
    0.1667
    0.3333
    0.2500
    0.2500
    0.3333
    0.1667
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div></div></div><div class="inlineWrapper"><div  class = 'S10'></div></div></div><div  class = 'S11'><span></span></div><h2  class = 'S2'><span>案例3  线性混合整数规划</span></h2><div  class = 'S3'><span></span></div><div  class = 'S4'><span texencoding="\min \quad  -2 x_{1}-x_{2}-4 x_{3}-3 x_{4}-x_{5} \\
 \begin{cases}
2 x_{2}+x_{3}+4 x_{4}+2 x_{5} \leq 54 \\
3 x_{1}+4 x_{2}+5 x_{3}-x_{4}-x_{5} \leq  62 \\
x_{1}, x_{2} \geq  0, x_{3} \geq  3.32, x_{4} \geq 0.678, x_{5} \geq  2.57
\end{cases}" style="vertical-align:-45px"><img src="" width="332" height="104.5" /></span></div><ul  class = 'S12'><li  class = 'S13'><span>上述问题,决策变量均为整数, 则最优解 [19,0,4,10,5]  最优解 -89</span></li><li  class = 'S13'><span>如1,4,5 决策变量是整数, 则最优解 [19,0,3.8,11, 3]  最优解 -89.2</span></li><li  class = 'S13'><span>来自 《薛定宇的最优化计算》 6-3</span></li></ul><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%% 6-3 上述问题,决策变量均为整数, 则最优解 [19,0,4,10,5]  最优解 -89</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc; clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>; yalmip(</span><span style="color: rgb(160, 32, 240);">'clear'</span><span>); </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>A = [0, 2 , 1, 4, 2;</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    3, 4, 5, -1,-1];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>b = [54; 62];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>ul = [0,0,3.32, 0.678, 2.57]';</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>x = intvar(5,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>const = [A*x&lt;=b, x&gt;=ul];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>obj = -[2,1,4,3,1] *x;</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>options = sdpsettings(</span><span style="color: rgb(160, 32, 240);">'verbose'</span><span>,1,</span><span style="color: rgb(160, 32, 240);">'solver'</span><span>,</span><span style="color: rgb(160, 32, 240);">'gurobi'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>sol = optimize(const,obj,options)</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="930F3C57" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_5" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="713" data-hashorizontaloverflow="false"><div class="textElement">Academic license - for non-commercial use only - expires 2021-07-16
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 7 rows, 5 columns and 14 nonzeros
Model fingerprint: 0xa4510ff3
Variable types: 0 continuous, 5 integer (0 binary)
Coefficient statistics:
  Matrix range     [1e+00, 5e+00]
  Objective range  [1e+00, 4e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [7e-01, 6e+01]
Presolve removed 5 rows and 1 columns
Presolve time: 0.00s
Presolved: 2 rows, 4 columns, 7 nonzeros
Variable types: 0 continuous, 4 integer (0 binary)
Found heuristic solution: objective -88.0000000

Root relaxation: objective -8.933333e+01, 2 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0  -89.33333    0    1  -88.00000  -89.33333  1.52%     -    0s
H    0     0                     -89.0000000  -89.33333  0.37%     -    0s
     0     0  -89.33333    0    1  -89.00000  -89.33333  0.37%     -    0s

Explored 1 nodes (2 simplex iterations) in 0.14 seconds
Thread count was 4 (of 4 available processors)

Solution count 2: -89 -88 
No other solutions better than -89

Optimal solution found (tolerance 1.00e-04)
Best objective -8.900000000000e+01, best bound -8.900000000000e+01, gap 0.0000%</div></div><div class="inlineElement eoOutputWrapper embeddedOutputsVariableStringElement" uid="6A1BA83B" contenteditable="false" data-testid="output_6" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="152" data-hashorizontaloverflow="false"><div class="textElement"><div><span class="variableNameElement">sol = <span class="headerElement">包含以下字段的 struct:</span></span></div><div>    yalmipversion: '20210331'
    matlabversion: '9.8.0.1323502 (R2020a)'
       yalmiptime: 0.4863
       solvertime: 0.7982
             info: 'Successfully solved (GUROBI-GUROBI)'
          problem: 0
</div></div></div></div></div><div class="inlineWrapper"><div  class = 'S9'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>sol.problem == 0 </span><span style="color: rgb(60, 118, 61);">% problem =0 代表求解成功</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(obj)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    disp(</span><span style="color: rgb(160, 32, 240);">'求解出错'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="892A23FA" contenteditable="false" data-testid="output_7" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">ans = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">5×1</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:65,&quot;totalColumns&quot;:1,&quot;totalRows&quot;:5,&quot;charsPerColumn&quot;:6}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 67px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">    19
     0
     4
    10
     5
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; '>ans = -89</div></div></div><div class="inlineWrapper"><div  class = 'S10'></div></div></div><div  class = 'S11'><span></span></div><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%% 6-3 混合整数规划 如1,4,5 决策变量是整数, 则最优解 [19,0,3.8,11, 3]  最优解 -89.2</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc; clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>; yalmip(</span><span style="color: rgb(160, 32, 240);">'clear'</span><span>); </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 设置决策变量 类型</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>x1 = intvar(3,1)</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement" uid="1C98EDC2" contenteditable="false" data-testid="output_9" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="44" data-hashorizontaloverflow="false"><div class="textElement">Linear matrix variable 3x1 (full, real, integer, 3 variables)
Coeffiecient range: 1 to 1</div></div></div></div><div class="inlineWrapper outputs"><div  class = 'S14'><span style="white-space: pre;"><span>x2 = sdpvar(2,1)</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement" uid="DE198E97" contenteditable="false" data-testid="output_10" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="44" data-hashorizontaloverflow="false"><div class="textElement">Linear matrix variable 2x1 (full, real, 2 variables)
Coeffiecient range: 1 to 1</div></div></div></div><div class="inlineWrapper"><div  class = 'S9'><span style="white-space: pre;"><span>x = [x1(1); x2; x1(2:3) ];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 系数</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>A = [0, 2 , 1, 4, 2;</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    3, 4, 5, -1,-1];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>b = [54; 62];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>ul = [0,0,3.32, 0.678, 2.57]';</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>const = [A*x&lt;=b, x&gt;=ul];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>obj = -[2,1,4,3,1] *x;</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>sol = optimize(const,obj)</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="AC277649" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_11" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="692" data-hashorizontaloverflow="false"><div class="textElement">Academic license - for non-commercial use only - expires 2021-07-16
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 7 rows, 5 columns and 14 nonzeros
Model fingerprint: 0xaa93f0bb
Variable types: 2 continuous, 3 integer (0 binary)
Coefficient statistics:
  Matrix range     [1e+00, 5e+00]
  Objective range  [1e+00, 4e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [7e-01, 6e+01]
Presolve removed 5 rows and 0 columns
Presolve time: 0.00s
Presolved: 2 rows, 5 columns, 9 nonzeros
Variable types: 2 continuous, 3 integer (0 binary)

Root relaxation: objective -8.944667e+01, 2 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0  -89.44667    0    2          -  -89.44667      -     -    0s
H    0     0                     -89.2000000  -89.44667  0.28%     -    0s
     0     0     cutoff    0       -89.20000  -89.20000  0.00%     -    0s

Explored 1 nodes (4 simplex iterations) in 0.04 seconds
Thread count was 4 (of 4 available processors)

Solution count 1: -89.2 
No other solutions better than -89.2

Optimal solution found (tolerance 1.00e-04)
Best objective -8.920000000000e+01, best bound -8.920000000000e+01, gap 0.0000%</div></div><div class="inlineElement eoOutputWrapper embeddedOutputsVariableStringElement" uid="41DF4479" contenteditable="false" data-testid="output_12" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="152" data-hashorizontaloverflow="false"><div class="textElement"><div><span class="variableNameElement">sol = <span class="headerElement">包含以下字段的 struct:</span></span></div><div>    yalmipversion: '20210331'
    matlabversion: '9.8.0.1323502 (R2020a)'
       yalmiptime: 1.5793
       solvertime: 0.7315
             info: 'Successfully solved (GUROBI-GUROBI)'
          problem: 0
</div></div></div></div></div><div class="inlineWrapper"><div  class = 'S9'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>sol.problem == 0 </span><span style="color: rgb(60, 118, 61);">% problem =0 代表求解成功</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(obj)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    disp(</span><span style="color: rgb(160, 32, 240);">'求解出错'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="BB0AF7E0" contenteditable="false" data-testid="output_13" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">ans = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">5×1</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:108,&quot;totalColumns&quot;:1,&quot;totalRows&quot;:5,&quot;charsPerColumn&quot;:10}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 110px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">   19.0000
         0
    3.8000
   11.0000
    3.0000
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; '>ans = -89.2000</div></div></div><div class="inlineWrapper"><div  class = 'S10'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%% </span></span></div></div></div><div  class = 'S11'><span></span></div><h2  class = 'S2'><span>案例4 非线性混合整数规划</span></h2><div  class = 'S3'><span></span></div><div  class = 'S4'><span texencoding="\min \quad x_{1}^{2}+x_{2}^{2}+2 x_{3}^{2}+x_{4}^{2}-5 x_{1}-5 x_{2}-21 x_{3}+7 x_{4} \\
\begin{cases}
-x_{1}^{2}-x_{2}^{2}-x_{3}^{2}-x_{4}^{2}-x_{1}+x_{2}-x_{3}+x_{4}+8 \geq 0 \\
-x_{1}^{2}-2 x_{2}^{2}-x_{3}^{2}-2 x_{4}^{2}+x_{1}+x_{4}+10 \geq  0 \\
-2 x_{1}^{2}-x_{2}^{2}-x_{3}^{2}-2 x_{4}^{2}+x_{2}+x_{4}+5 \geq  0
\end{cases}" style="vertical-align:-53px"><img src="" width="380.5" height="156" /></span></div><div  class = 'S4'><span></span></div><ul  class = 'S12'><li  class = 'S13'><span>《来自薛定宇matlab 卷5最优化计算 》 6-4</span></li></ul><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc; clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>; close </span><span style="color: rgb(160, 32, 240);">all</span><span>; yalmip(</span><span style="color: rgb(160, 32, 240);">'clear'</span><span>); </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define variables</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>x = intvar(4,1);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%lb = [0,0,0,0];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define constraints </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Constraints = </span><span class="warning_squiggle_rte376400421">[</span><span>x(1)^2 + x(2)^2 + x(3)^2 + x(4)^2 + x(1) - x(2) + x(3) - x(4) - 8 &lt;= 0];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Constraints = [Constraints, x(1)^2 + 2*x(2)^2 + x(3)^2 + 2*x(4)^2 - x(1) - x(4) - 10 &lt;=0];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Constraints = [Constraints, 2*x(1)^2 + x(2)^2 + x(3)^2 + 2*x(4)^2 - x(2) - x(4) - 5 &lt;=0];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Constraints = </span><span class="warning_squiggle_rte376400421">[</span><span>Constraints];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Define an objective</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>Objective =  x(1)^2 + x(2)^2 + 2*x(3)^2 + x(4)^2 - 5*x(1) - 5*x(2) - 21*x(3) + 7*x(4);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% 设置求解器 </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%全局最优，比较慢且使用与少数变量，使用gurobi不用担心解释器的问题啦</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%options = sdpsettings('solver','bmibnb','bmibnb.upper','fmincon'); </span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%options = sdpsettings('solver','bnb','bnb.solver','fmincon');</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>options = sdpsettings(</span><span style="color: rgb(160, 32, 240);">'solver'</span><span>,</span><span style="color: rgb(160, 32, 240);">'gurobi'</span><span>);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">% Solve the problem</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>sol = optimize(Constraints,Objective,options);</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="62731334" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_15" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="1005" data-hashorizontaloverflow="false"><div class="textElement">Academic license - for non-commercial use only - expires 2021-07-16
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 18 rows, 22 columns and 46 nonzeros
Model fingerprint: 0xbcd4af16
Model has 4 quadratic objective terms
Model has 3 quadratic constraints
Variable types: 18 continuous, 4 integer (0 binary)
Coefficient statistics:
  Matrix range     [5e-01, 1e+00]
  QMatrix range    [1e+00, 1e+00]
  Objective range  [5e+00, 2e+01]
  QObjective range [2e+00, 4e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [2e+00, 6e+00]
Presolve removed 12 rows and 12 columns
Presolve time: 0.00s
Presolved: 27 rows, 43 columns, 71 nonzeros
Presolved model has 4 quadratic objective terms
Presolved model has 15 quadratic constraint(s)
Variable types: 31 continuous, 12 integer (0 binary)

Root relaxation: objective -7.987500e+01, 49 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0  -79.87500    0   12          -  -79.00000      -     -    0s
     0     0  -68.18741    0   12          -  -68.18741      -     -    0s
     0     0  -47.98419    0   12          -  -47.98419      -     -    0s
     0     0  -45.30187    0   12          -  -45.30187      -     -    0s
     0     0  -42.93069    0   12          -  -42.93069      -     -    0s
     0     0  -42.63328    0   12          -  -42.63328      -     -    0s
     0     0  -42.63328    0   12          -  -42.63328      -     -    0s
     0     0  -42.53839    0   12          -  -42.53839      -     -    0s
     0     0  -42.53839    0   12          -  -42.53839      -     -    0s
     0     0  -42.51319    0   12          -  -42.51319      -     -    0s
H    0     0                     -38.0000000  -42.51319  11.9%     -    0s
     0     2  -42.51319    0   12  -38.00000  -42.51319  11.9%     -    0s

Explored 10 nodes (268 simplex iterations) in 0.14 seconds
Thread count was 4 (of 4 available processors)

Solution count 1: -38 
No other solutions better than -38

Optimal solution found (tolerance 1.00e-04)
Best objective -3.800000000000e+01, best bound -3.800000000000e+01, gap 0.0000%</div></div></div></div><div class="inlineWrapper"><div  class = 'S9'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>sol.problem == 0 </span><span style="color: rgb(60, 118, 61);">% problem =0 代表求解成功</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(Objective)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    disp(</span><span style="color: rgb(160, 32, 240);">'求解出错'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="8F0FC526" contenteditable="false" data-testid="output_16" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">ans = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">4×1</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:65,&quot;totalColumns&quot;:1,&quot;totalRows&quot;:4,&quot;charsPerColumn&quot;:6}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 67px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">     0
     1
     2
     0
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; '>ans = -38</div></div></div></div><div  class = 'S11'><span></span></div><div  class = 'S3'><span></span></div><h2  class = 'S2'><span>案例5  非线性混合整数规划</span></h2><div  class = 'S3'><span></span></div><div  class = 'S4'><span> </span><span texencoding="Max \quad z=x_1^{2} +x_2^{2} +3x_3^{2} +4x_{4}^{2} + 2x_{5}^{2} -8x_1 -2x_2 -3x_3 - 2x_{5}\\
\text { s.t. }\begin{cases}
0&lt;= x_i &lt;=99 ( i =1,2, \ldots, 5)\\
x_1+x_2+x_3+x_4+x_5&lt;=400\\
x_1 + 2x_2 + 2x_3 + x_4 +6x_5 &lt;=800\\

2x_1 +x_2 + 6x_3 &lt;=800\\
x_3 + x_4 +5x_5&lt;=200
\end{cases}" style="vertical-align:-73px"><img src="" width="461.5" height="169.5" /></span></div><ul  class = 'S12'><li  class = 'S13'><span>最优解: </span><span style=' font-family: monospace;'>[ 53, 99, 99, 99 , 0]</span><span>, 对应的目标函数值  </span><span style=' font-family: monospace;'>-80199</span></li><li  class = 'S13'><span>问题来源于: </span><a href = "https://www.ilovematlab.cn/thread-302727-1-1.html"><span style=' text-decoration: underline;'>https://www.ilovematlab.cn/thread-302727-1-1.html</span></a></li></ul><div class="CodeBlock"><div class="inlineWrapper"><div  class = 'S5'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%% 混合非线性整数规划</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>clc, clear </span><span class="warning_squiggle_rte376400421" style="color: rgb(160, 32, 240);">all</span><span>;</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>yalmip(</span><span style="color: rgb(160, 32, 240);">'clear'</span><span>)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>x=intvar(1,5);</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>A=[1 1 1 1 1;1 2 2 1 6;2 1 6 0 0;0 0 1 1 5];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>b=[400;800;800;200];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>f= - [1 1 3 4 2]*(x'.^2) + [8 2 3 1 2]*x';</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>F=[A*x'&lt;=b, 0&lt;=</span><span class="warning_squiggle_rte376400421">x</span><span>&lt;=99];</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(60, 118, 61);">%options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span>sol = optimize(F,f);</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsTextElement scrollableOutput" uid="302B228A" data-scroll-top="null" data-scroll-left="null" contenteditable="false" data-testid="output_18" style="width: 1569.05px; max-height: 261px;" data-width="1540" data-height="943" data-hashorizontaloverflow="false"><div class="textElement">Academic license - for non-commercial use only - expires 2021-07-16
Gurobi Optimizer version 9.1.2 build v9.1.2rc0 (mac64)
Thread count: 2 physical cores, 4 logical processors, using up to 4 threads
Optimize a model with 14 rows, 5 columns and 26 nonzeros
Model fingerprint: 0x3a450b07
Model has 5 quadratic objective terms
Variable types: 0 continuous, 5 integer (0 binary)
Coefficient statistics:
  Matrix range     [1e+00, 6e+00]
  Objective range  [1e+00, 8e+00]
  QObjective range [2e+00, 8e+00]
  Bounds range     [0e+00, 0e+00]
  RHS range        [1e+02, 8e+02]
Found heuristic solution: objective 0.0000000
Presolve removed 11 rows and 0 columns
Presolve time: 0.00s
Presolved: 13 rows, 441 columns, 888 nonzeros
Variable types: 0 continuous, 441 integer (436 binary)

Root relaxation: objective -8.271370e+04, 7 iterations, 0.00 seconds

    Nodes    |    Current Node    |     Objective Bounds      |     Work
 Expl Unexpl |  Obj  Depth IntInf | Incumbent    BestBd   Gap | It/Node Time

     0     0 -82713.700    0    4    0.00000 -82713.700      -     -    0s
H    0     0                    -77823.00000 -82713.700  6.28%     -    0s
H    0     0                    -80199.00000 -82713.700  3.14%     -    0s
     0     0 -82268.773    0    8 -80199.000 -82268.773  2.58%     -    0s
     0     0 -80550.500    0    3 -80199.000 -80550.500  0.44%     -    0s
     0     0     cutoff    0      -80199.000 -80199.000  0.00%     -    0s

Cutting planes:
  Gomory: 1
  Implied bound: 1
  MIR: 2
  StrongCG: 1

Explored 1 nodes (48 simplex iterations) in 0.11 seconds
Thread count was 4 (of 4 available processors)

Solution count 4: -80199 -79242 -77823 0 
No other solutions better than -80199

Optimal solution found (tolerance 1.00e-04)
Best objective -8.019900000000e+04, best bound -8.019900000000e+04, gap 0.0000%</div></div></div></div><div class="inlineWrapper"><div  class = 'S9'></div></div><div class="inlineWrapper"><div  class = 'S6'></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">if </span><span>sol.problem == 0 </span><span style="color: rgb(60, 118, 61);">% problem =0 代表求解成功</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(x)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    value(f)</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">else</span></span></div></div><div class="inlineWrapper"><div  class = 'S6'><span style="white-space: pre;"><span>    disp(</span><span style="color: rgb(160, 32, 240);">'求解出错'</span><span>);</span></span></div></div><div class="inlineWrapper outputs"><div  class = 'S7'><span style="white-space: pre;"><span style="color: rgb(0, 0, 255);">end</span></span></div><div  class = 'S8'><div class="inlineElement eoOutputWrapper embeddedOutputsVariableMatrixElement" uid="90903390" contenteditable="false" data-testid="output_19" style="width: 1569.05px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;" data-width="1540"><div class="matrixElement veSpecifier" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="veVariableName variableNameElement double" style="width: 1540px; white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="headerElementClickToInteract" style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><span style="white-space: normal; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">ans = </span><span class="veVariableValueSummary veMetaSummary" style="white-space: normal; font-style: normal; color: rgb(179, 179, 179); font-size: 17.9104px;">1×5</span></div></div><div class="valueContainer" data-layout="{&quot;columnWidth&quot;:65,&quot;totalColumns&quot;:5,&quot;totalRows&quot;:1,&quot;charsPerColumn&quot;:6}" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"><div class="variableValue" style="width: 327px; white-space: pre; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;">    53    99    99    99     0
</div><div class="horizontalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div><div class="verticalEllipsis hide" style="white-space: nowrap; font-style: normal; color: rgb(64, 64, 64); font-size: 17.9104px;"></div></div></div></div><div class='variableElement' style='font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 17.9104px; '>ans = -80199</div></div></div><div class="inlineWrapper"><div  class = 'S10'></div></div></div></div>
<br>
<!-- 
##### SOURCE BEGIN #####
%% Yalmip 利用 gurobi求解器 （mac） 
% 
%% 案例1
% 
% 
% $$max  \quad \frac{ x_{1} + x_{2}}{ (2x_1 +x_2)} \\s.t.  \begin{cases}\quad  
% x_1 + x_2 \geq 2\\\quad x_2 - x_1 \leq 1\\ \quad x_1 \leq 1\end{cases}$$
% 
% 

%% 案例1 
% 清除工作区
clc; clear all; close all;
% 创建决策变量
x = sdpvar(1,2);
% 添加约束条件
C = [
    x(1) + x(2)  >= 2
    x(2)-x(1) <=1
    x(1)<=1
    ];
% 配置
ops = sdpsettings('verbose',1,'solver','gurobi');
% 目标函数
z = -(x(1)+2*x(2))/(2*x(1)+x(2)); % 注意这是求解最大值
% 求解
reuslt = optimize(C,z);
if reuslt.problem == 0 % problem =0 代表求解成功
    value(x)
    -value(z)   % 反转
else
    disp('求解出错');
end

%% 
% 
%% 案例2 
%% 
% * 这是yalmip官网的例子，不在给出显示表达式
%% 
% 

%%
clc; clear all; close all; yalmip('clear');
% Define variables
x = sdpvar(10,1);

% Define constraints 
Constraints = [sum(x) <= 10, x(1) == 0, 0.5 <= x(2) <= 1.5];
for i = 1 : 7
  Constraints = [Constraints, x(i) + x(i+1) <= x(i+2) + x(i+3)];
end

% Define an objective
Objective = x'*x+norm(x,1);

% Set some options for YALMIP and solver
options = sdpsettings('verbose',1,'solver','gurobi');

% Solve the problem
sol = optimize(Constraints,Objective,options);

% Analyze error flags
if sol.problem == 0
 % Extract and display value
 solution = value(x)
else
 display('Hmm, something went wrong!');
 sol.info
 yalmiperror(sol.problem)
end

%% 
% 
%% 案例3  线性混合整数规划
% 
% 
% $$\min \quad  -2 x_{1}-x_{2}-4 x_{3}-3 x_{4}-x_{5} \\ \begin{cases}2 x_{2}+x_{3}+4 
% x_{4}+2 x_{5} \leq 54 \\3 x_{1}+4 x_{2}+5 x_{3}-x_{4}-x_{5} \leq  62 \\x_{1}, 
% x_{2} \geq  0, x_{3} \geq  3.32, x_{4} \geq 0.678, x_{5} \geq  2.57\end{cases}$$
%% 
% * 上述问题,决策变量均为整数, 则最优解 [19,0,4,10,5]  最优解 -89
% * 如1,4,5 决策变量是整数, 则最优解 [19,0,3.8,11, 3]  最优解 -89.2
% * 来自 《薛定宇的最优化计算》 6-3

%% 6-3 上述问题,决策变量均为整数, 则最优解 [19,0,4,10,5]  最优解 -89
clc; clear all; close all; yalmip('clear'); 

A = [0, 2 , 1, 4, 2;
    3, 4, 5, -1,-1];
b = [54; 62];
ul = [0,0,3.32, 0.678, 2.57]';
x = intvar(5,1);
const = [A*x<=b, x>=ul];
obj = -[2,1,4,3,1] *x;
options = sdpsettings('verbose',1,'solver','gurobi');
sol = optimize(const,obj,options)

if sol.problem == 0 % problem =0 代表求解成功
    value(x)
    value(obj)
else
    disp('求解出错');
end

%% 
% 

%% 6-3 混合整数规划 如1,4,5 决策变量是整数, 则最优解 [19,0,3.8,11, 3]  最优解 -89.2
clc; clear all; close all; yalmip('clear'); 

% 设置决策变量 类型
x1 = intvar(3,1)
x2 = sdpvar(2,1)
x = [x1(1); x2; x1(2:3) ];
% 系数
A = [0, 2 , 1, 4, 2;
    3, 4, 5, -1,-1];
b = [54; 62];
ul = [0,0,3.32, 0.678, 2.57]';
const = [A*x<=b, x>=ul];
obj = -[2,1,4,3,1] *x;
sol = optimize(const,obj)

if sol.problem == 0 % problem =0 代表求解成功
    value(x)
    value(obj)
else
    disp('求解出错');
end
%% 
%% 
% 
%% 案例4 非线性混合整数规划
% 
% 
% $$\min \quad x_{1}^{2}+x_{2}^{2}+2 x_{3}^{2}+x_{4}^{2}-5 x_{1}-5 x_{2}-21 
% x_{3}+7 x_{4} \\\begin{cases}-x_{1}^{2}-x_{2}^{2}-x_{3}^{2}-x_{4}^{2}-x_{1}+x_{2}-x_{3}+x_{4}+8 
% \geq 0 \\-x_{1}^{2}-2 x_{2}^{2}-x_{3}^{2}-2 x_{4}^{2}+x_{1}+x_{4}+10 \geq  0 
% \\-2 x_{1}^{2}-x_{2}^{2}-x_{3}^{2}-2 x_{4}^{2}+x_{2}+x_{4}+5 \geq  0\end{cases}$$
% 
% 
%% 
% * 《来自薛定宇matlab 卷5最优化计算 》 6-4




clc; clear all; close all; yalmip('clear'); 
% Define variables
x = intvar(4,1);
%lb = [0,0,0,0];
% Define constraints 
Constraints = [x(1)^2 + x(2)^2 + x(3)^2 + x(4)^2 + x(1) - x(2) + x(3) - x(4) - 8 <= 0];

Constraints = [Constraints, x(1)^2 + 2*x(2)^2 + x(3)^2 + 2*x(4)^2 - x(1) - x(4) - 10 <=0];
Constraints = [Constraints, 2*x(1)^2 + x(2)^2 + x(3)^2 + 2*x(4)^2 - x(2) - x(4) - 5 <=0];
Constraints = [Constraints];
% Define an objective
Objective =  x(1)^2 + x(2)^2 + 2*x(3)^2 + x(4)^2 - 5*x(1) - 5*x(2) - 21*x(3) + 7*x(4);

% 设置求解器 
%全局最优，比较慢且使用与少数变量，使用gurobi不用担心解释器的问题啦
%options = sdpsettings('solver','bmibnb','bmibnb.upper','fmincon'); 
%options = sdpsettings('solver','bnb','bnb.solver','fmincon');
options = sdpsettings('solver','gurobi');


% Solve the problem
sol = optimize(Constraints,Objective,options);

if sol.problem == 0 % problem =0 代表求解成功
    value(x)
    value(Objective)
else
    disp('求解出错');
end
%% 
% 
% 
% 
%% 案例5  非线性混合整数规划
% 
% 
% $$Max \quad z=x_1^{2} +x_2^{2} +3x_3^{2} +4x_{4}^{2} + 2x_{5}^{2} -8x_1 -2x_2 
% -3x_3 - 2x_{5}\\\text { s.t. }\begin{cases}0<= x_i <=99 ( i =1,2, \ldots, 5)\\x_1+x_2+x_3+x_4+x_5<=400\\x_1 
% + 2x_2 + 2x_3 + x_4 +6x_5 <=800\\2x_1 +x_2 + 6x_3 <=800\\x_3 + x_4 +5x_5<=200\end{cases}$$
%% 
% * 最优解: |[ 53, 99, 99, 99 , 0]|, 对应的目标函数值  |-80199|
% * 问题来源于: <https://www.ilovematlab.cn/thread-302727-1-1.html https://www.ilovematlab.cn/thread-302727-1-1.html>


%% 混合非线性整数规划
clc, clear all;
yalmip('clear')

x=intvar(1,5);
A=[1 1 1 1 1;1 2 2 1 6;2 1 6 0 0;0 0 1 1 5];
b=[400;800;800;200];
f= - [1 1 3 4 2]*(x'.^2) + [8 2 3 1 2]*x';
F=[A*x'<=b, 0<=x<=99];
%options = sdpsettings('solver','bmibnb', 'bmibnb.uppersolver', 'fmincon');

sol = optimize(F,f);


if sol.problem == 0 % problem =0 代表求解成功
    value(x)
    value(f)
else
    disp('求解出错');
end
##### SOURCE END #####
--></body></html>